let text = document.getElementById("geshou");
let audio = document.getElementById("audio");
let select = document.getElementById("select");
let shangyishou = document.getElementById("shangyishou");
let xiayishou = document.getElementById("xiayishou");
let options = document.querySelectorAll("#select>option");
let zanting = document.getElementById("zanting");
let zan = false;
console.log(options);

let sings = [
  { src: "./img/001.mp3" },
  { src: "./img/002.mp3" },
  { src: "./img/003.ogg" },
  { src: "./img/004.ogg" },
];
//封装options添加selelctd默认选中
function selected() {
  for (let j = 0; j < options.length; j++) {
    if (audio.dataset.num == options[j].value) {
      options[j].selected = true;
      text.innerHTML = options[j].innerText;
    } else {
      options[j].selected = false;
    }
  }
}
//播放完然后直接播放下一首歌曲
audio.onended = function () {
  for (let i = 0; i < sings.length; i++) {
    if (audio.dataset.num == i && i != sings.length - 1) {
      audio.src = sings[i + 1].src;
      audio.dataset.num = i + 1;
      selected();
      break;
    } else if (audio.dataset.num == sings.length - 1) {
      audio.src = sings[i].src;
      audio.dataset.num = i;
      selected();
      break;
    }
  }
};
//点击上一首
shangyishou.onclick = function () {
  for (let i = 0; i < sings.length; i++) {
    if (audio.dataset.num == 0) {
      audio.src = sings[sings.length - 1].src;
      audio.dataset.num = sings.length - 1;
      selected();
      break;
    } else if (parseInt(audio.dataset.num) == i && i != 0) {
      audio.src = sings[i - 1].src;
      audio.dataset.num = i - 1;
      selected();
      break;
    }
  }
};
//点击下一首
xiayishou.onclick = function () {
  // console.log(audio.dataset.num);
  for (let i = 0; i < sings.length; i++) {
    if (parseInt(audio.dataset.num) == i) {
      audio.src = sings[i + 1].src;
      audio.dataset.num = i + 1;
      selected();
      break;
    } else if (audio.dataset.num == sings.length - 1) {
      audio.src = sings[i].src;
      audio.dataset.num = i;
      selected();
      break;
    }
  }
};
//点击下拉列表里面的歌曲
select.onchange = function () {
  for (let i = 0; i < sings.length; i++) {
    if (this.value == i) {
      audio.src = sings[i].src;
      audio.dataset.num = this.value;
    }
  }
};

zanting.onclick = () => {
  zan = !zan;
  if (zan == false) {
    audio.pause();
    console.log(0000);
  }else{
    audio.play();
  }

  /* zan ?:audio.pause();*/
};
